#ifndef __ARRAY_H__
#define __ARRAY_H__

#include <stddef.h>
#include <assert.h>

template <class T>
class DynamicArray
{
    public:
        DynamicArray(int size=1024);
        ~DynamicArray();

        bool Add(T);
        bool Del(int index);
        void Resize();
        int MaxSize(){ return this->max_size; }
        int Size() { return this->iPos; }

    private:
        int max_size;
        T * pArray;
        int iPos;
};

template <class T>
DynamicArray<T>::DynamicArray(int size)
{
    this->max_size = size;
    this->iPos = 0;
    this->pArray = new T[size];
}


template <class T>
DynamicArray<T>::~DynamicArray()
{
    if (this->pArray != NULL)
    {
        delete [] pArray;
        pArray = NULL;
        this->iPos = -1;
        this->max_size = -1;
    }
}


template <class T>
bool DynamicArray<T>::Add(T item)
{
    if (this->iPos == this->max_size)
        Resize();

    this->pArray[this->iPos] = item;
    this->iPos++;
    return true;
}


template <class T>
bool DynamicArray<T>::Del(int index)
{
    assert(iPos > 0);
    assert(index < iPos && index >= 0);
    this->pArray[index] = this->pArray[iPos - 1];
    this->iPos--;
    return true;
}

template <class T>
void DynamicArray<T>::Resize()
{
    this->max_size += this->max_size >> 1;
    T* newArray = new T[this->max_size];
    for (int i=0; i<this->iPos; i++)
    {
        newArray[i] = this->pArray[i];
    }

    delete [] pArray;
    pArray = newArray;
}


#endif
